<template>
  <BasicDrawer
    v-bind="$attrs"
    @register="registerDrawer"
    showFooter
    :title="getTitle"
    width="500px"
    @ok="handleSubmit"
  >
    <BasicForm @register="registerForm">
      <template #filename="{ field }">
        <Upload
          :maxCount="1"
          :before-upload="(file) => handleBeforeUploadFilename(file, field)"
          accept="image/*"
          :file-list="fileList"
          @remove="(file): any => handleRemove(file, field)"
        >
          <a-button type="primary">
            <upload-outlined />
            上传
          </a-button>
        </Upload>
      </template>
    </BasicForm>
  </BasicDrawer>
</template>
<script lang="ts">
  import { defineComponent, ref, computed, unref } from 'vue';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { formSchema } from './sealList.data';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import { Upload } from 'ant-design-vue';
  import type { UploadProps } from 'ant-design-vue';
  import { getStampCreate, getStampUpdate } from '/@/api/flowManage/flowManage';

  export default defineComponent({
    name: 'SealListDrawer',
    components: { BasicDrawer, BasicForm, Upload },
    emits: ['success', 'register'],
    setup(_, { emit }) {
      const isUpdate = ref(true);
      let dataRef = ref<any>({});
      const fileList = ref<UploadProps['fileList']>([]);
      const [registerForm, { resetFields, setFieldsValue, validate, updateSchema }] = useForm({
        labelWidth: 110,
        schemas: formSchema,
        showActionButtonGroup: false,
      });

      const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
        resetFields();
        setDrawerProps({ confirmLoading: false });
        isUpdate.value = !!data?.isUpdate;
        dataRef.value = {};
        fileList.value = [];
        if (unref(isUpdate)) {
          dataRef.value = data?.record;
          setFieldsValue({
            ...unref(dataRef),
          });
        } else {
          dataRef.value = { kind: 'default', pwd: '111111' };
        }
        setFieldsValue({
          ...unref(dataRef),
        });
      });

      const getTitle = computed(() => (!unref(isUpdate) ? '新增' : '编辑'));

      async function handleSubmit() {
        try {
          const values = await validate();
          let formDate = Object.assign({}, unref(dataRef), values);
          setDrawerProps({ confirmLoading: true });
          console.log(formDate);
          let Files = new FormData();
          for (let v in formDate) {
            Files.append(v, formDate[v] ? formDate[v] : '');
          }
          if (unref(isUpdate)) {
            // await getStampUpdate(Files);
          } else {
            await getStampCreate(Files);
          }
          closeDrawer();
          emit('success');
        } finally {
          setDrawerProps({ confirmLoading: false });
        }
      }

      const handleBeforeUploadFilename = (file, filed) => {
        setFieldsValue({ [`${filed}`]: file });
        fileList.value = [file];
        return false;
      };
      const handleRemove = (file, filed) => {
        return new Promise((resolve) => {
          fileList.value = [];
          setFieldsValue({ [`${filed}`]: '' });
          resolve(true);
        });
      };
      return {
        registerDrawer,
        registerForm,
        getTitle,
        handleSubmit,
        handleBeforeUploadFilename,
        fileList,
        handleRemove,
      };
    },
  });
</script>
